﻿using System;
using System.Runtime.InteropServices;

namespace SkinableFormsLib
{
    public class Win32
    {

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        public enum HitTestValues:int
        {
            HTERROR = -2,
            HTTRANSPARENT = -1,
            HTNOWHERE = 0,
            HTCLIENT = 1,
            HTCAPTION = 2,
            HTSYSMENU = 3,
            HTGROWBOX = 4,
            HTMENU = 5,
            HTHSCROLL = 6,
            HTVSCROLL = 7,
            HTMINBUTTON = 8,
            HTMAXBUTTON = 9,
            HTLEFT = 10,
            HTRIGHT = 11,
            HTTOP = 12,
            HTTOPLEFT = 13,
            HTTOPRIGHT = 14,
            HTBOTTOM = 15,
            HTBOTTOMLEFT = 16,
            HTBOTTOMRIGHT = 17,
            HTBORDER = 18,
            HTOBJECT = 19,
            HTCLOSE = 20,
            HTHELP = 21
        }
        public enum SysCommands : int
        {
            SC_SIZE = 0xF000,
            SC_MOVE = 0xF010,
            SC_MINIMIZE = 0xF020,
            SC_MAXIMIZE = 0xF030,
            SC_NEXTWINDOW = 0xF040,
            SC_PREVWINDOW = 0xF050,
            SC_CLOSE = 0xF060,
            SC_VSCROLL = 0xF070,
            SC_HSCROLL = 0xF080,
            SC_MOUSEMENU = 0xF090,
            SC_KEYMENU = 0xF100,
            SC_ARRANGE = 0xF110,
            SC_RESTORE = 0xF120,
            SC_TASKLIST = 0xF130,
            SC_SCREENSAVE = 0xF140,
            SC_HOTKEY = 0xF150,
            //#if(WINVER >= 0x0400) //Win95
            SC_DEFAULT = 0xF160,
            SC_MONITORPOWER = 0xF170,
            SC_CONTEXTHELP = 0xF180,
            SC_SEPARATOR = 0xF00F,
            //#endif /* WINVER >= 0x0400 */

            //#if(WINVER >= 0x0600) //Vista
            SCF_ISSECURE = 0x00000001,
            //#endif /* WINVER >= 0x0600 */

            /*
              * Obsolete names
              */
            SC_ICON = SC_MINIMIZE,
            SC_ZOOM = SC_MAXIMIZE,
        }


        public enum MKS : int
        {
            MK_LBUTTON = 0x0001,
            MK_RBUTTON = 0x0002,
            MK_SHIFT = 0x0004,
            MK_CONTROL = 0x0008,
            MK_MBUTTON = 0x0010,
            MK_ALT = 0x1000
        }

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        public static bool PostMessageSafe(HandleRef hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool returnValue = PostMessage(hWnd, msg, wParam, lParam);

            if (returnValue == false)
            {
                // An error occured
                throw new Exception(Marshal.GetLastWin32Error().ToString());
            }

            return returnValue;
        }        


        public enum TernaryRasterOperations : uint
        {
            /// <summary>dest = source</summary>
            SRCCOPY = 0x00CC0020,
            /// <summary>dest = source OR dest</summary>
            SRCPAINT = 0x00EE0086,
            /// <summary>dest = source AND dest</summary>
            SRCAND = 0x008800C6,
            /// <summary>dest = source XOR dest</summary>
            SRCINVERT = 0x00660046,
            /// <summary>dest = source AND (NOT dest)</summary>
            SRCERASE = 0x00440328,
            /// <summary>dest = (NOT source)</summary>
            NOTSRCCOPY = 0x00330008,
            /// <summary>dest = (NOT src) AND (NOT dest)</summary>
            NOTSRCERASE = 0x001100A6,
            /// <summary>dest = (source AND pattern)</summary>
            MERGECOPY = 0x00C000CA,
            /// <summary>dest = (NOT source) OR dest</summary>
            MERGEPAINT = 0x00BB0226,
            /// <summary>dest = pattern</summary>
            PATCOPY = 0x00F00021,
            /// <summary>dest = DPSnoo</summary>
            PATPAINT = 0x00FB0A09,
            /// <summary>dest = pattern XOR dest</summary>
            PATINVERT = 0x005A0049,
            /// <summary>dest = (NOT dest)</summary>
            DSTINVERT = 0x00550009,
            /// <summary>dest = BLACK</summary>
            BLACKNESS = 0x00000042,
            /// <summary>dest = WHITE</summary>
            WHITENESS = 0x00FF0062
        }
        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth,
           int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);





        public const int RDW_INVALIDATE = 0x0001;
        public const int RDW_INTERNALPAINT = 0x0002;
        public const int RDW_ERASE = 0x0004;

        public const int RDW_VALIDATE = 0x0008;
        public const int RDW_NOINTERNALPAINT = 0x0010;
        public const int RDW_NOERASE = 0x0020;

        public const int RDW_NOCHILDREN = 0x0040;
        public const int RDW_ALLCHILDREN = 0x0080;

        public const int RDW_UPDATENOW = 0x0100;
        public const int RDW_ERASENOW = 0x0200;

        public const int RDW_FRAME = 0x0400;
        public const int RDW_NOFRAME = 0x0800;

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RedrawWindow(IntPtr hWnd, [In] ref RECT lprcUpdate, IntPtr hrgnUpdate, uint flags);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags);

        public enum Bool
        {
            False = 0,
            True
        };


        [StructLayout(LayoutKind.Sequential)]
        public struct Point
        {
            public Int32 x;
            public Int32 y;

            public Point(Int32 x, Int32 y) { this.x = x; this.y = y; }
        }


        [StructLayout(LayoutKind.Sequential)]
        public struct Size
        {
            public Int32 cx;
            public Int32 cy;

            public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
        }


        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct ARGB
        {
            public byte Blue;
            public byte Green;
            public byte Red;
            public byte Alpha;
        }


        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct BLENDFUNCTION
        {
            public byte BlendOp;
            public byte BlendFlags;
            public byte SourceConstantAlpha;
            public byte AlphaFormat;
        }


        public const Int32 ULW_COLORKEY = 0x00000001;
        public const Int32 ULW_ALPHA = 0x00000002;
        public const Int32 ULW_OPAQUE = 0x00000004;

        public const byte AC_SRC_OVER = 0x00;
        public const byte AC_SRC_ALPHA = 0x01;


        public const Int32 WM_ACTIVATE = 0x0006;
        public const Int32 WM_ACTIVATEAPP = 0x001C;
        public const Int32 WM_AFXFIRST = 0x0360;
        public const Int32 WM_AFXLAST = 0x037F;
        public const Int32 WM_APP = 0x8000;
        public const Int32 WM_ASKCBFORMATNAME = 0x030C;
        public const Int32 WM_CANCELJOURNAL = 0x004B;
        public const Int32 WM_CANCELMODE = 0x001F;
        public const Int32 WM_CAPTURECHANGED = 0x0215;
        public const Int32 WM_CHANGECBCHAIN = 0x030D;
        public const Int32 WM_CHANGEUISTATE = 0x0127;
        public const Int32 WM_CHAR = 0x0102;
        public const Int32 WM_CHARTOITEM = 0x002F;
        public const Int32 WM_CHILDACTIVATE = 0x0022;
        public const Int32 WM_CLEAR = 0x0303;
        public const Int32 WM_CLOSE = 0x0010;
        public const Int32 WM_COMMAND = 0x0111;
        public const Int32 WM_COMPACTING = 0x0041;
        public const Int32 WM_COMPAREITEM = 0x0039;
        public const Int32 WM_CONTEXTMENU = 0x007B;
        public const Int32 WM_COPY = 0x0301;
        public const Int32 WM_COPYDATA = 0x004A;
        public const Int32 WM_CREATE = 0x0001;
        public const Int32 WM_CTLCOLORBTN = 0x0135;
        public const Int32 WM_CTLCOLORDLG = 0x0136;
        public const Int32 WM_CTLCOLOREDIT = 0x0133;
        public const Int32 WM_CTLCOLORLISTBOX = 0x0134;
        public const Int32 WM_CTLCOLORMSGBOX = 0x0132;
        public const Int32 WM_CTLCOLORSCROLLBAR = 0x0137;
        public const Int32 WM_CTLCOLORSTATIC = 0x0138;
        public const Int32 WM_CUT = 0x0300;
        public const Int32 WM_DEADCHAR = 0x0103;
        public const Int32 WM_DELETEITEM = 0x002D;
        public const Int32 WM_DESTROY = 0x0002;
        public const Int32 WM_DESTROYCLIPBOARD = 0x0307;
        public const Int32 WM_DEVICECHANGE = 0x0219;
        public const Int32 WM_DEVMODECHANGE = 0x001B;
        public const Int32 WM_DISPLAYCHANGE = 0x007E;
        public const Int32 WM_DRAWCLIPBOARD = 0x0308;
        public const Int32 WM_DRAWITEM = 0x002B;
        public const Int32 WM_DROPFILES = 0x0233;
        public const Int32 WM_ENABLE = 0x000A;
        public const Int32 WM_ENDSESSION = 0x0016;
        public const Int32 WM_ENTERIDLE = 0x0121;
        public const Int32 WM_ENTERMENULOOP = 0x0211;
        public const Int32 WM_ENTERSIZEMOVE = 0x0231;
        public const Int32 WM_ERASEBKGND = 0x0014;
        public const Int32 WM_EXITMENULOOP = 0x0212;
        public const Int32 WM_EXITSIZEMOVE = 0x0232;
        public const Int32 WM_FONTCHANGE = 0x001D;
        public const Int32 WM_GETDLGCODE = 0x0087;
        public const Int32 WM_GETFONT = 0x0031;
        public const Int32 WM_GETHOTKEY = 0x0033;
        public const Int32 WM_GETICON = 0x007F;
        public const Int32 WM_GETMINMAXINFO = 0x0024;
        public const Int32 WM_GETOBJECT = 0x003D;
        public const Int32 WM_GETTEXT = 0x000D;
        public const Int32 WM_GETTEXTLENGTH = 0x000E;
        public const Int32 WM_HANDHELDFIRST = 0x0358;
        public const Int32 WM_HANDHELDLAST = 0x035F;
        public const Int32 WM_HELP = 0x0053;
        public const Int32 WM_HOTKEY = 0x0312;
        public const Int32 WM_HSCROLL = 0x0114;
        public const Int32 WM_HSCROLLCLIPBOARD = 0x030E;
        public const Int32 WM_ICONERASEBKGND = 0x0027;
        public const Int32 WM_IME_CHAR = 0x0286;
        public const Int32 WM_IME_COMPOSITION = 0x010F;
        public const Int32 WM_IME_COMPOSITIONFULL = 0x0284;
        public const Int32 WM_IME_CONTROL = 0x0283;
        public const Int32 WM_IME_ENDCOMPOSITION = 0x010E;
        public const Int32 WM_IME_KEYDOWN = 0x0290;
        public const Int32 WM_IME_KEYLAST = 0x010F;
        public const Int32 WM_IME_KEYUP = 0x0291;
        public const Int32 WM_IME_NOTIFY = 0x0282;
        public const Int32 WM_IME_REQUEST = 0x0288;
        public const Int32 WM_IME_SELECT = 0x0285;
        public const Int32 WM_IME_SETCONTEXT = 0x0281;
        public const Int32 WM_IME_STARTCOMPOSITION = 0x010D;
        public const Int32 WM_INITDIALOG = 0x0110;
        public const Int32 WM_INITMENU = 0x0116;
        public const Int32 WM_INITMENUPOPUP = 0x0117;
        public const Int32 WM_INPUTLANGCHANGE = 0x0051;
        public const Int32 WM_INPUTLANGCHANGEREQUEST = 0x0050;
        public const Int32 WM_KEYDOWN = 0x0100;
        public const Int32 WM_KEYFIRST = 0x0100;
        public const Int32 WM_KEYLAST = 0x0108;
        public const Int32 WM_KEYUP = 0x0101;
        public const Int32 WM_KILLFOCUS = 0x0008;
        public const Int32 WM_LBUTTONDBLCLK = 0x0203;
        public const Int32 WM_LBUTTONDOWN = 0x0201;
        public const Int32 WM_LBUTTONUP = 0x0202;
        public const Int32 WM_MBUTTONDBLCLK = 0x0209;
        public const Int32 WM_MBUTTONDOWN = 0x0207;
        public const Int32 WM_MBUTTONUP = 0x0208;
        public const Int32 WM_MDIACTIVATE = 0x0222;
        public const Int32 WM_MDICASCADE = 0x0227;
        public const Int32 WM_MDICREATE = 0x0220;
        public const Int32 WM_MDIDESTROY = 0x0221;
        public const Int32 WM_MDIGETACTIVE = 0x0229;
        public const Int32 WM_MDIICONARRANGE = 0x0228;
        public const Int32 WM_MDIMAXIMIZE = 0x0225;
        public const Int32 WM_MDINEXT = 0x0224;
        public const Int32 WM_MDIREFRESHMENU = 0x0234;
        public const Int32 WM_MDIRESTORE = 0x0223;
        public const Int32 WM_MDISETMENU = 0x0230;
        public const Int32 WM_MDITILE = 0x0226;
        public const Int32 WM_MEASUREITEM = 0x002C;
        public const Int32 WM_MENUCHAR = 0x0120;
        public const Int32 WM_MENUCOMMAND = 0x0126;
        public const Int32 WM_MENUDRAG = 0x0123;
        public const Int32 WM_MENUGETOBJECT = 0x0124;
        public const Int32 WM_MENURBUTTONUP = 0x0122;
        public const Int32 WM_MENUSELECT = 0x011F;
        public const Int32 WM_MOUSEACTIVATE = 0x0021;
        public const Int32 WM_MOUSEFIRST = 0x0200;
        public const Int32 WM_MOUSEHOVER = 0x02A1;
        public const Int32 WM_MOUSELAST = 0x020D;
        public const Int32 WM_MOUSELEAVE = 0x02A3;
        public const Int32 WM_MOUSEMOVE = 0x0200;
        public const Int32 WM_MOUSEWHEEL = 0x020A;
        public const Int32 WM_MOUSEHWHEEL = 0x020E;
        public const Int32 WM_MOVE = 0x0003;
        public const Int32 WM_MOVING = 0x0216;
        public const Int32 WM_NCACTIVATE = 0x0086;
        public const Int32 WM_NCCALCSIZE = 0x0083;
        public const Int32 WM_NCCREATE = 0x0081;
        public const Int32 WM_NCDESTROY = 0x0082;
        public const Int32 WM_NCHITTEST = 0x0084;
        public const Int32 WM_NCLBUTTONDBLCLK = 0x00A3;
        public const Int32 WM_NCLBUTTONDOWN = 0x00A1;
        public const Int32 WM_NCLBUTTONUP = 0x00A2;
        public const Int32 WM_NCMBUTTONDBLCLK = 0x00A9;
        public const Int32 WM_NCMBUTTONDOWN = 0x00A7;
        public const Int32 WM_NCMBUTTONUP = 0x00A8;
        public const Int32 WM_NCMOUSEMOVE = 0x00A0;
        public const Int32 WM_NCPAINT = 0x0085;
        public const Int32 WM_NCRBUTTONDBLCLK = 0x00A6;
        public const Int32 WM_NCRBUTTONDOWN = 0x00A4;
        public const Int32 WM_NCRBUTTONUP = 0x00A5;
        public const Int32 WM_NEXTDLGCTL = 0x0028;
        public const Int32 WM_NEXTMENU = 0x0213;
        public const Int32 WM_NOTIFY = 0x004E;
        public const Int32 WM_NOTIFYFORMAT = 0x0055;
        public const Int32 WM_NULL = 0x0000;
        public const Int32 WM_PAINT = 0x000F;
        public const Int32 WM_PAINTCLIPBOARD = 0x0309;
        public const Int32 WM_PAINTICON = 0x0026;
        public const Int32 WM_PALETTECHANGED = 0x0311;
        public const Int32 WM_PALETTEISCHANGING = 0x0310;
        public const Int32 WM_PARENTNOTIFY = 0x0210;
        public const Int32 WM_PASTE = 0x0302;
        public const Int32 WM_PENWINFIRST = 0x0380;
        public const Int32 WM_PENWINLAST = 0x038F;
        public const Int32 WM_POWER = 0x0048;
        public const Int32 WM_POWERBROADCAST = 0x0218;
        public const Int32 WM_PRINT = 0x0317;
        public const Int32 WM_PRINTCLIENT = 0x0318;
        public const Int32 WM_QUERYDRAGICON = 0x0037;
        public const Int32 WM_QUERYENDSESSION = 0x0011;
        public const Int32 WM_QUERYNEWPALETTE = 0x030F;
        public const Int32 WM_QUERYOPEN = 0x0013;
        public const Int32 WM_QUEUESYNC = 0x0023;
        public const Int32 WM_QUIT = 0x0012;
        public const Int32 WM_RBUTTONDBLCLK = 0x0206;
        public const Int32 WM_RBUTTONDOWN = 0x0204;
        public const Int32 WM_RBUTTONUP = 0x0205;
        public const Int32 WM_RENDERALLFORMATS = 0x0306;
        public const Int32 WM_RENDERFORMAT = 0x0305;
        public const Int32 WM_SETCURSOR = 0x0020;
        public const Int32 WM_SETFOCUS = 0x0007;
        public const Int32 WM_SETFONT = 0x0030;
        public const Int32 WM_SETHOTKEY = 0x0032;
        public const Int32 WM_SETICON = 0x0080;
        public const Int32 WM_SETREDRAW = 0x000B;
        public const Int32 WM_SETTEXT = 0x000C;
        public const Int32 WM_SETTINGCHANGE = 0x001A;
        public const Int32 WM_SHOWWINDOW = 0x0018;
        public const Int32 WM_SIZE = 0x0005;
        public const Int32 WM_SIZECLIPBOARD = 0x030B;
        public const Int32 WM_SIZING = 0x0214;
        public const Int32 WM_SPOOLERSTATUS = 0x002A;
        public const Int32 WM_STYLECHANGED = 0x007D;
        public const Int32 WM_STYLECHANGING = 0x007C;
        public const Int32 WM_SYNCPAINT = 0x0088;
        public const Int32 WM_SYSCHAR = 0x0106;
        public const Int32 WM_SYSCOLORCHANGE = 0x0015;
        public const Int32 WM_SYSCOMMAND = 0x0112;
        public const Int32 WM_SYSDEADCHAR = 0x0107;
        public const Int32 WM_SYSKEYDOWN = 0x0104;
        public const Int32 WM_SYSKEYUP = 0x0105;
        public const Int32 WM_TCARD = 0x0052;
        public const Int32 WM_TIMECHANGE = 0x001E;
        public const Int32 WM_TIMER = 0x0113;
        public const Int32 WM_UNDO = 0x0304;
        public const Int32 WM_UNINITMENUPOPUP = 0x0125;
        public const Int32 WM_USER = 0x0400;
        public const Int32 WM_USERCHANGED = 0x0054;
        public const Int32 WM_VKEYTOITEM = 0x002E;
        public const Int32 WM_VSCROLL = 0x0115;
        public const Int32 WM_VSCROLLCLIPBOARD = 0x030A;
        public const Int32 WM_WINDOWPOSCHANGED = 0x0047;
        public const Int32 WM_WINDOWPOSCHANGING = 0x0046;
        public const Int32 WM_WININICHANGE = 0x001A;
        public const Int32 WM_XBUTTONDBLCLK = 0x020D;
        public const Int32 WM_XBUTTONDOWN = 0x020B;
        public const Int32 WM_XBUTTONUP = 0x020C;






        public const Int32 PRF_CHILDREN = 0x0010;
        public const Int32 PRF_CLIENT = 0x0004;

        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
        public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);

        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
        public static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("user32.dll", ExactSpelling = true)]
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

        [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
        public static extern Bool DeleteDC(IntPtr hdc);

        [DllImport("gdi32.dll", ExactSpelling = true)]
        public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

        [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
        public static extern Bool DeleteObject(IntPtr hObject);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        //[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        //static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, StringBuilder lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, String lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern void SendMessage(HandleRef hWnd, uint msg, IntPtr wParam, ref RECT lParam);
        //[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        //static extern IntPtr SendMessage(HandleRef hWnd, uint msg, IntPtr wParam, ref POINT lParam);









    }
}
